package com.luych.oauth2Server.service;

import com.luych.common.constant.SpringCacheConstant;
import com.luych.common.page.Page;
import com.luych.oauth2Server.dao.Oauth2ClientMapper;
import com.luych.oauth2Server.dao.Oauth2ScopeMapper;
import com.luych.oauth2Server.entity.Oauth2Client;
import com.luych.oauth2Server.entity.Oauth2ClientExample;
import com.luych.oauth2Server.entity.Oauth2Scope;
import com.luych.oauth2Server.entity.Oauth2ScopeExample;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class Oauth2Service {

    private final Oauth2ClientMapper oauth2ClientMapper;
    private final Oauth2ScopeMapper oauth2ScopeMapper;

    @Autowired
    public Oauth2Service(Oauth2ClientMapper oauth2ClientMapper, Oauth2ScopeMapper oauth2ScopeMapper) {
        this.oauth2ClientMapper = oauth2ClientMapper;
        this.oauth2ScopeMapper = oauth2ScopeMapper;
    }

    public Page pageClient(String keywords, Page page) {
        Oauth2ClientExample example = new Oauth2ClientExample();
        if (!StringUtils.isEmpty(keywords)) {
            example.or().andClientIdLike(keywords);
            example.or().andRedirectUrlLike(keywords);
        }
        page.setRecordCnt(oauth2ClientMapper.countByExample(example));
        example.setOffset(page.getPageStart());
        example.setLimit(page.getPageSize());
        if (!StringUtils.isEmpty(page.getSortName()) && !StringUtils.isEmpty(page.getSortOrder())) {
            example.setOrderByClause(page.getSortName() + " " + page.getSortOrder());
        }
        page.setRecords(oauth2ClientMapper.selectByExample(example));
        return page;
    }

    @Cacheable(cacheNames = SpringCacheConstant.oauth2Server_client, key = "#oauth2Client.clientId")
    public Oauth2Client addClient(Oauth2Client oauth2Client) {
        oauth2ClientMapper.insertSelective(oauth2Client);
        return oauth2ClientMapper.selectByPrimaryKey(oauth2Client.getClientId());
    }

    @CachePut(cacheNames = SpringCacheConstant.oauth2Server_client, key = "#oauth2Client.clientId")
    public Oauth2Client modifyClient(Oauth2Client oauth2Client) {
        oauth2ClientMapper.updateByPrimaryKey(oauth2Client);
        return oauth2ClientMapper.selectByPrimaryKey(oauth2Client.getClientId());
    }

    @CacheEvict(cacheNames = SpringCacheConstant.oauth2Server_client, key = "#clientId")
    public void removeClient(String clientId) {
        oauth2ClientMapper.deleteByPrimaryKey(clientId);
    }

    @Cacheable(cacheNames = SpringCacheConstant.oauth2Server_client, key = "#clientId")
    public Oauth2Client getByClientId(String clientId) {
        return oauth2ClientMapper.selectByPrimaryKey(clientId);
    }

    public List<Oauth2Scope> getScopeByClientId(String clientId) {
        Oauth2Client oauth2Client = getByClientId(clientId);
        String scopeIds = oauth2Client.getGrantScopeIds();
        if (StringUtils.isEmpty(scopeIds)) {
            return new ArrayList<>(0);
        }
        List<Oauth2Scope> scopeList = new ArrayList<>();
        for (String scopeId : scopeIds.split(",")) {
            scopeList.add(oauth2ScopeMapper.selectByPrimaryKey(scopeId));
        }
        return scopeList;
    }

    public String getScopeCodeByClientId(String clientId) {
        List<Oauth2Scope> scopeList = getScopeByClientId(clientId);
        StringBuilder stringBuilder = new StringBuilder();
        for (Oauth2Scope scope : scopeList) {
            stringBuilder.append(scope).append(",");
        }
        return stringBuilder.substring(0, stringBuilder.length() - 1);
    }

    public List<Oauth2Scope> listScope() {
        return oauth2ScopeMapper.selectByExample(new Oauth2ScopeExample());
    }
}
